 var app;
 
 $(document).ready(function() {
	
	app = new LineFilter(); 
	 
	//-------------------------------------------------------------------
	//
	// load additional script files
	//
	//-------------------------------------------------------------------  
	
	jQuery.getScript( path() + 'js/jquery.download.js');
	jQuery.getScript( path() + 'js/utf8.js');
	jQuery.getScript( path() + 'js/DropTarget.js', function() {
		new DropTarget( $('textarea')[0], {onDisplay: app.delegate(app, app.parseInput)} );
	});
	jQuery.getScript( path() + 'js/jquery.textchange.js', function() {
		$('textarea').bind( 'hastext', app.delegate(app, app.parseInput) );
		$('textarea').bind( 'notext', app.delegate(app, app.clear) );
	});
	
	//-------------------------------------------------------------------
	//
	// init UI
	//
	//-------------------------------------------------------------------
 
	$('.button.btn-showTags').click(function(e) { 
		e.preventDefault();  
		app.toggleCodes();
	});
  
	$('.button.btn-showLineNums').click(function(e) { 
		e.preventDefault();  
		app.toggleLineNums();
	});
	
	$('.btn-loadDefaults').click(function(e) {
		e.preventDefault();
		app.loadDefaults();
	}); 

	$('.btn-saveAsSrt').click(function(e) {
		e.preventDefault();
		app.saveAsSrt();
	}); 
	
	$('textarea').bind('clear', app.delegate(app, app.clear));
	setTimeout(app.onresize, 100);
	$(window).resize(app.onresize);  
	
	app.clear();
	
});

var LineFilter = function() { 
	this.showLineNums = true;
	this.showCodes = false;
}
LineFilter.prototype = {
	delegate: function( obj, fn ) {
		return function() { return fn.apply(obj, arguments); }
	},
	loadDefaults: function() {  
		$('textarea').load( path() + 'sample-files/1403problema_eng_fra_ger_compleV1.0.txt', this.delegate(this, this.parseInput) ); 
		return '...loading default files.';
	}, 
	clear: function() { 
		$('textarea').html('');
		$('.app-buttons').hide();
		$('.lang-toggles').empty(); 
	},
	parseInput: function() {  
		var self = this; 
		var language_codes = [];
		this.original_lines = $('textarea').html().split('\n');
	 
		this.languages = {};
		$(this.original_lines).each(function(i,line) {  
			if (line.charAt(2) == '/') {
				var code = line.substr(0,2); 
				if (jQuery.inArray(code, language_codes) == -1) {
					language_codes.push(code); 
				}
			} 
		});  
		$(language_codes).each(function(i,lang) {
			self.languages[lang] = {code: lang, show: true};
		})
		
		// create UI

		
		$('.app-buttons').show();
		//$('.button.btn-showTags').addClass('active');
		//$('.button.btn-showTags input').attr('checked', true);
		
		$('.button.btn-showLineNums').show();
		$('.button.btn-showLineNums').addClass('active');
		$('.button.btn-showLineNums input').attr('checked', true);
		
		$('.lang-toggles').empty(); 
		var addButton = function(lang){
			btn = $('<a>')
			.attr({
				'class': 'active button lang-'+lang,
				'href': '#'
			})
			.click(function(e) {
				e.preventDefault();
				self.toggleLanguage(lang);
			})
			.appendTo($('.lang-toggles')); 
			btn.append( $('<span>') );
			btn.find('span').append( $('<input type="checkbox" checked="checked">') );
			btn.find('span').append( $('<span>').text(lang) );
		}
		for (var lang in this.languages) {
			addButton(lang);
		}

		this.updateOutput();
	},
	toggleLineNums: function() {
		var btn = $('.button.btn-showLineNums'); 
		btn.toggleClass('active');
		setTimeout(function() {
			btn.find('input').attr('checked',  btn.hasClass('active') );
		}, 25)
		this.showLineNums =  btn.hasClass('active');
		this.updateOutput(); 
		
	},
	toggleCodes: function(value) {
		var btn = $('.button.btn-showTags'); 
		btn.toggleClass('active');
		setTimeout(function() {
			btn.find('input').attr('checked',  btn.hasClass('active') );
		}, 25)
		this.showCodes =  btn.hasClass('active');
		this.updateOutput(); 
	}, 
	toggleLanguage: function(lang) {
		var btn = $('.button.lang-'+lang); 
		btn.toggleClass('active');
		setTimeout(function() {
			btn.find('input').attr('checked',  btn.hasClass('active') );
		}, 25);
		this.languages[lang].show = btn.hasClass('active');
		this.updateOutput(); 
	},
	updateOutput: function() {
		var self = this;
		var lines = [];
		var sub_lines = 0;
		$(this.original_lines).each(function(i,l) {	 
			var skipLine = false; 
			var line = l+'';
			
			if (line.charAt(2) == '/') {
				var code = line.substr(0,2); 
				if (code === 'tc' && self.showLineNums===true) {
					lines.push((sub_lines++)+1);
				}
				if (self.showCodes === false) { 
					line = line.replace(code+'/', ''); 
				} 
				if (self.languages[code].show == false) {
					skipLine = true;
				}
			} 
			if (!skipLine) { 
				lines.push( line );
			}
		}); 
		$('textarea').html( lines.join('\n') );
	},
	 
	onresize: function() {
		$('table').each(function(i,table) {
			table = $(table);
			var table_height = $(window).height() - table.position().top;			 
			table.height( table_height );			
			table.find('.panel').height(  table_height - 20  )
			table.find('textarea').height(  table_height - 100  )
		}); 
	}, 
	  
	saveAsSrt: function() { 
		jQuery.download( path()+'download.php', 'name=temp.srt&content='+ urlencode(  $('textarea').html() ), 'post');
	}
};
